﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace Cloud.Boss
{
    public class Program
    {
        public static void Main(string[] args)
        {
            // BuildWebHost(args).Run();
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
           Host.CreateDefaultBuilder(args)


               //.ConfigureServices((context, services) =>
               //{
               //    services.Configure<KestrelServerOptions>(
               //        context.Configuration.GetSection("Kestrel"));
               //})
               .ConfigureWebHostDefaults(webBuilder =>
               {
                   //参考：https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/servers/kestrel?view=aspnetcore-3.1
                   webBuilder.ConfigureKestrel(serverOptions =>
                   {
                       //最大连接数，默认情况下，最大连接数不受限制；
                       serverOptions.Limits.MaxConcurrentConnections = 300;
                       serverOptions.Limits.MaxConcurrentUpgradedConnections = 300;
                       //请求正文最大大小,默认的请求正文最大大小为 30,000,000 字节，大约 28.6 MB
                       serverOptions.Limits.MaxRequestBodySize = 10 * 1024;

                       // 请求正文最小数据速率,默认的最小速率为 240 字节/秒，包含 5 秒的宽限期
                       serverOptions.Limits.MinRequestBodyDataRate =
                           new MinDataRate(bytesPerSecond: 100,
                               gracePeriod: TimeSpan.FromSeconds(10));
                       serverOptions.Limits.MinResponseDataRate =
                           new MinDataRate(bytesPerSecond: 100,
                               gracePeriod: TimeSpan.FromSeconds(10));
                       //绑定到 TCP 套接字
                       //serverOptions.Listen(IPAddress.Loopback, 5000);
                       //serverOptions.Listen(IPAddress.Loopback, 5001,
                       //    listenOptions =>
                       //    {
                       //        listenOptions.UseHttps("testCert.pfx",
                       //            "testPassword");
                       //    });
                       //超时时间，默认两分钟
                       serverOptions.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(2);
                       //请求Header超时,获取或设置服务器接收请求标头所花费的最大时间量。 默认值为 30 秒。  
                       serverOptions.Limits.RequestHeadersTimeout = TimeSpan.FromMinutes(1);
                   });
                   //默认使用的是kestrel服务器

                   webBuilder.UseStartup<Startup>();
                   webBuilder.UseUrls("http://*:8080");
               });
    }


}
